//
//  SharePopup.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 2/21/14.
//  Copyright (c) 2014 Nguyen Tan Loc. All rights reserved.
//

#import "SharePopup.h"
#import "AppDelegate.h"
#import "LevelManager.h"
#import "DefineText.h"
#import "UserInfo.h"


@implementation SharePopup

static SharePopup *m_Instance = nil;

+(SharePopup *) getInstance
{
    if (m_Instance == nil)
        m_Instance = [[SharePopup alloc] init];
    return m_Instance;
}

-(id) init
{
    if (self = [super init])
    {
        mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
        isFBAvailable = [SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook];
        isTWAvailable = [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter];
    }
    
    return self;
}

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (isTWAvailable == YES && isFBAvailable == YES)
    {
        if (buttonIndex == 1)
            [self share:SLServiceTypeFacebook];
        else if (buttonIndex == 2)
            [self share:SLServiceTypeTwitter];
    }
    else if (isFBAvailable == YES && isTWAvailable == NO)
    {
        if (buttonIndex == 1)
            [self share:SLServiceTypeFacebook];
    }
    else if (isTWAvailable == YES && isFBAvailable == NO)
    {
        if (buttonIndex == 1)
            [self share:SLServiceTypeTwitter];
    }
    

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:YES forKey:mKey];
    [defaults synchronize];
}

-(void) share:(NSString *)serviceType
{
    
    AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
    mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:serviceType];
    [mySLComposerSheet setInitialText:[NSString stringWithFormat:TEXT_SHARE, [self getLastLevel], @"https://itunes.apple.com/us/app/1path/id827139182?ls=1&mt=8"]]; //the message you want to post
    
    [app.navController presentViewController:mySLComposerSheet animated:YES completion:nil];
    
    [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
        switch (result) {
            case SLComposeViewControllerResultCancelled:
                
                break;
            case SLComposeViewControllerResultDone:
                [self addItem];
                break;
            default:
                break;
        }
    }];
}

-(void) showSharePopup
{
    
    mKey = [self getKey];
    if ([mKey isEqual:@""])
        return;
    
    // check if they shared
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:mKey] != nil)
        return;
    
    UIAlertView *alert;
    if (isFBAvailable == YES && isTWAvailable == YES)
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Congratulation!"
                                                        message:[NSString stringWithFormat:TEXT_SHARE_REQUEST, [self getItemName]]
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Facebook", @"Twitter", nil];
    }
    else if (isFBAvailable == YES && isTWAvailable == NO)
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Congratulation!"
                                           message:[NSString stringWithFormat:TEXT_SHARE_REQUEST, [self getItemName]]
                                          delegate:self
                                 cancelButtonTitle:@"Cancel"
                                 otherButtonTitles:@"Facebook", nil];
    }
    else if (isFBAvailable == NO && isTWAvailable == YES)
    {
        alert = [[UIAlertView alloc] initWithTitle:@"Congratulation!"
                                           message:[NSString stringWithFormat:TEXT_SHARE_REQUEST, [self getItemName]]
                                          delegate:self
                                 cancelButtonTitle:@"Cancel"
                                 otherButtonTitles:@"Twitter", nil];
    }
    else
        return;
    
    [alert show];
    [alert release];
}

-(NSString *) getKey
{
    if ([LevelManager getInstance]._currentLevel == 10)
    {
        return KEY_SHARE_10;
    }
    else if ([LevelManager getInstance]._currentLevel == 20)
    {
        return KEY_SHARE_20;
    }
    else if ([LevelManager getInstance]._currentLevel == 30)
    {
        return KEY_SHARE_30;
    }
    else if ([LevelManager getInstance]._currentLevel == 40)
    {
        return KEY_SHARE_40;
    }
    
    return @"";
}

-(NSString *) getItemName
{
    if ([LevelManager getInstance]._currentLevel == 10)
    {
        return TEXT_ITEM_NAME_SHIELD;
    }
    else if ([LevelManager getInstance]._currentLevel == 20)
    {
        return TEXT_ITEM_NAME_TIMER;
    }
    else if ([LevelManager getInstance]._currentLevel == 30)
    {
        return TEXT_ITEM_NAME_MAGNET;
    }
    else if ([LevelManager getInstance]._currentLevel == 40)
    {
        return TEXT_ITEM_NAME_SUN;
    }
    
    return @"";
}

-(void) addItem
{
    if ([LevelManager getInstance]._currentLevel == 10)
    {
        [UserInfo getInstance]._numItemGlove += 1;
    }
    else if ([LevelManager getInstance]._currentLevel == 20)
    {
        [UserInfo getInstance]._numItemTime += 1;
    }
    else if ([LevelManager getInstance]._currentLevel == 30)
    {
        [UserInfo getInstance]._numItemMagnet += 1;
    }
    else if ([LevelManager getInstance]._currentLevel == 40)
    {
        [UserInfo getInstance]._numItemXray += 1;
    }
    
    [[UserInfo getInstance] saveUserInfo];
    [[NSNotificationCenter defaultCenter] postNotificationName:EVENT_ITEM_CHANGED object:nil];
}

-(int) getLastLevel
{
    return [LevelManager getInstance]._currentLevel;
}

@end
